<template>
  <center>
    <h2>步骤状态查询</h2>
  </center>
  <center>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" style="width: 500px">
<!--      <el-form-item label="步骤编号" prop="stepno">-->
<!--        <el-input v-model="ruleForm.stepno"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="步骤编号" prop="stepno">
        <el-select v-model="ruleForm.stepno" placeholder="请选择单元编号" style="width: 400px">
          <el-option
              v-for="item in ruleForm.stepnoList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="各个单元状态" prop="partstate">
        <el-input v-model="ruleForm.partstate" readonly="true" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="完整步骤状态" prop="allstate">
        <el-input v-model="ruleForm.allstate" readonly="true" type="textarea"></el-input>
      </el-form-item>
    </el-form>
  </center>

  <center>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('ruleForm')">查询状态</el-button>
      <el-button type="danger" @click="onCancel()">取消</el-button>
    </el-form-item>
  </center>
</template>

<script>
import {get} from "@/utils/http";
import {ElMessage} from "element-plus";

export default {
  name: "CheckStepState",
  data() {
    return {
      ruleForm: {
        stepno: '',
        partstate: '',
        allstate: '',
        stepnoList: []
      },
      rules: {
        stepno:[
          { required: true, message: '请输入步骤编号', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.getOptions();
  },
  methods: {
    getOptions(){
      let that = this;
      get('/getonestepnojs', {
      // get('/getunitnojs', {
      //   stepno: that.ruleForm.stepno
        // unitno: that.ruleForm.stepno
      }).then(function (res) {
        if (res.code === 1) {
          // that.ruleForm.partstate = res.data.length;
          that.ruleForm.stepnoList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.stepnoList.push({value: res.data[i].stepno, label: res.data[i].stepno});
            // that.ruleForm.stepnoList.push({value: res.data[i].unitno, label: res.data[i].unitno});
            // that.ruleForm.stepnoList = [{value: res.data[i].stepno, label: res.data[i].stepno}];
          }

          ElMessage.success({
            showClose: true,
            message: "查询成功",
            type: "success",
            center: true
          })
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    onSubmit(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          get('/checkstepstatejs', {
            stepno: that.ruleForm.stepno
          }).then(function (res) {
            if (res.code === 1) {
              var notbeginnum = 0;
              var backnum = 0;
              that.ruleForm.partstate = "";
              that.ruleForm.allstate = "";
              for(var i=0;i<res.data.length;i++){
                var temp1 = JSON.stringify(res.data[i].unitstate);
                that.ruleForm.partstate = that.ruleForm.partstate + JSON.stringify(res.data[i].unitno) + "单元状态:"
                    + temp1 + "\n";
                if(temp1 == "\"尚未开始\""){
                  notbeginnum = notbeginnum + 1;
                }
                else if(temp1 == "\"归还完成\""){
                  backnum = backnum + 1;
                  that.ruleForm.allstate = "步骤正在进行:功能单元" + JSON.stringify(res.data[i].unitno) + temp1;
                }
                else if(temp1 == "\"查验完成\"" || temp1 == "\"实验完成\""){
                  that.ruleForm.allstate = "步骤正在进行:功能单元" + JSON.stringify(res.data[i].unitno) + temp1;
                }
                else{
                  that.ruleForm.allstate = "步骤进行失败:功能单元" + JSON.stringify(res.data[i].unitno) + temp1;
                }
              }
              if(notbeginnum == res.data.length){
                that.ruleForm.allstate = "步骤尚未开始";
              }
              else if(backnum == res.data.length){
                that.ruleForm.allstate = "步骤已经完成";
              }

              ElMessage.success({
                showClose: true,
                message: "查询成功",
                type: "success",
                center: true
              })
            } else {
              ElMessage.warning({
                showClose: true,
                message: "查询失败",
                type: "warning",
                center: true
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel(){
      let that = this;
      let router_target = "MainPage";
      that.$router.push({
        name: router_target
      })
    }
  }
}
</script>

<style scoped>

</style>


